﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Kooboo.Web.Areas.Commerce.Controllers
{
    using Kooboo.Commerce;
    using Kooboo.Web.Mvc;
    using Kooboo.Globalization;
    using Kooboo.Commerce.Contexts.Shippings;
    
    [Authorize(Roles = "Administrator")]
    public class ShippingController : ManagementController
    {
        [HttpGet]
        public ActionResult Index()
        {
            ViewData["shippings"] = this.Actor.QueryShippings()
                .OrderByDescending(i=>i.Id)
                .Select(i => new
                {
                    i.Id,
                    i.Name,
                    Rules = i.ShippingCostRules
                    .Select(n=>new
                    {
                        n.Id,
                        n.From,
                        n.To,
                        n.Cost
                    })
                })
                .ToArray()
                .SerializeToJson();

            ViewData["delete_action"] = this.Url.Action<ShippingController>(i => i.Delete(null));
            ViewData["add_action"] = this.Url.Action<ShippingController>(i => i.Create(null));
            ViewData["edit_action"] = this.Url.Action<ShippingController>(i => i.Edit(null));
            

            return View();
        }




        [HttpPost]
        public ActionResult Create(IShipping shipping)
        {
            if (ModelState.IsValid)
            {
                this.Actor.Post(shipping);
                return Json(new
                {
                    Data = new
                    {
                        shipping.Id,
                        shipping.Name,
                        Rules = shipping.ShippingCostRules
                        .Select(n => new
                        {
                            n.Id,
                            n.From,
                            n.To,
                            n.Cost
                        })
                    }
                });
            }
            else
            {
                return Json(new { Message = ModelState.GetErrorMessages() });
            }
        }



        [HttpPost]
        public ActionResult Edit(IShipping shipping)
        {
            if (ModelState.IsValid)
            {
                this.Actor.Put(shipping);
                return Json(new
                {
                    Data = new
                        {
                            shipping.Id,
                            shipping.Name,
                            Rules = shipping.ShippingCostRules
                            .Select(n => new
                            {
                                n.Id,
                                n.From,
                                n.To,
                                n.Cost
                            })
                        }
                });
            }
            else
            {
                return Json(new { Message = ModelState.GetErrorMessages() });
            }
        }



        [HttpPost]
        public ActionResult Delete(IShipping shipping)
        {
            this.Actor.Delete(shipping);
            return Json(true);   
        }
  
    }
}
